package com.openscorer;

import java.util.ArrayList;

public class RangeMap {
	// Format "1/2:3,2/4:5"
	public RangeMap(String ranges) {
		ranges_ = new ArrayList<int[]>();
		String split_ranges[] = ranges.split(",");
		for (int i = 0; i < split_ranges.length; ++i){
			String range_and_val[] = split_ranges[i].split(":");
			String start_and_end[] = range_and_val[0].split("/");
			int range[] = new int[3];
			range[0] = Integer.parseInt(start_and_end[0]);
			range[1] = Integer.parseInt(start_and_end[1]);
			range[2] = Integer.parseInt(range_and_val[1]);
			ranges_.add(range);
		}
	}
	// This is stupidly linear, but whatever. Correctness over speed (for now)
	int map(int value) {
		for (int i = 0; i < ranges_.size(); ++i) {
			if (value >= ranges_.get(i)[0] && value <= ranges_.get(i)[1]) {
				return ranges_.get(i)[2];
			}
		}
		return 0;
	}
	ArrayList<int[]> ranges_;

}
